Lang.define ('Bs.util');
var D = document;
Bs.util.Html = {
	DIV		: 'div',
	FORM	: 'form',
	TABLE	: 'table',
	TBODY	: 'tbody',
	TR		: 'tr',
	TD		: 'td',
	INPUT	: 'input',
	isIterable : function(n){
		if(/NodeList|HTMLCollection/.test (Object.prototype.toString.call(v))){
			return true;
		}
		return ((n.nextNode || n.item) && Lang.isNumber (n.length));
	},
	iterate : function(obj, fn, scope) {
		if (isIterable (obj)) {
			Lang.loop (obj, fn, scope);
			return;
		} else if (Lang.isObject(obj)){
			for (var prop in obj){
				if (obj.hasOwnProperty(prop)) {
					if (fn.call(scope || obj, prop, obj[prop]) === false){
						return;
					};
				}
			}
		}
	},
	overwrite : function (dom, spec){
		dom = Sys.Device.dom (dom);
		dom.innerHTML = Bs.util.Html.create (spec);
	},
    create : function (spec){
	    var b = "",
	    	attr,
	    	val,
	    	key,
	    	keyVal,
	    	cn;

        if(Lang.isString (spec)){
            b = spec;
        } else if (Lang.isArray (spec)) {
	        Lang.loop (spec, function(v) {
                b += Bs.util.Html.create(v);
            });
        } else {
	        b += "<" + (spec.tag = spec.tag || DIV);
            iterate (spec, function(attr, val){
                if(!/tag|children|cn|html$/i.test(attr)){
                    if (Lang.isObject (val)) {
                        b += " " + attr + "='";
                        Bs.util.Html.iterate(val, function(key, keyVal){
                            b += key + ":" + keyVal + ";";
                        });
                        b += "'";
                    }else{
                        b += " " + ({cls : "class", htmlFor : "for"}[attr] || attr) + "='" + val + "'";
                    }
                }
            });
	        if (emptyTags.test (spec.tag)) {
	            b += "/>";
	        } else {
	            b += ">";
	            if ((cn = spec.children || spec.cn)) {
	                b += Bs.util.Html.create (cn);
	            } else if (spec.html){
	                b += spec.html;
	            }
	            b += "</" + spec.tag + ">";
        	}
        }
        return b;
    }
};
Html = Bs.util.Html;